package com.wanban.adutils.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.wanban.adutils.AdUtils
import com.wanban.adutils.db.bean.AdContent

@Database(
    entities =
    [
        AdContent::class
    ],
    version = 2
)
abstract class AdRoomUtils : RoomDatabase() {

    companion object {

        @Volatile
        private var INSTANCE: AdRoomUtils? = null

        fun getInstance(): AdRoomUtils {
            if (INSTANCE == null) {
                INSTANCE = synchronized(this) {
                    INSTANCE ?: buildDatabase(AdUtils.appContext).also { INSTANCE = it }
                }
            }
            return INSTANCE!!
        }

        private fun buildDatabase(context: Context) = Room.databaseBuilder(
            context.applicationContext, AdRoomUtils::class.java,
            "Ads.db"
        ).allowMainThreadQueries()
            .addMigrations(
                    MIGRATION_1_2()
//                    MIGRATION_2_3()
            )
            .fallbackToDestructiveMigrationOnDowngrade()
            .build()
    }

    abstract fun adDao(): AdDao
}
